<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ArraySplice</title>
</head>

<body>
    <div>打开浏览器控制台查看结果</div>
    <script>
        Array.prototype.ArraySplice = function (start, deleteCount) {
            var max = Math.max, //最大值
                min = Math.min, //最小值
                len = this.length, //当前数组长度
                insertIndex = max(arguments.length - 2, 0), //插入索引
                k = 0, //循环值
                new_len, //新数组长度
                result = [], //最终结果
                delta, //插入的索引值
                element, //数组每一项
                shift_count; //从删除开始的截取索引
            start = start || 0;
            // 当开始索引小于0时，与长度相加
            if (start < 0) start += len;
            //开始索引值的一个取值范围
            start = max(min(start, len), 0);
            // 删除数组项的一个取值范围
            deleteCount = max(
                min(typeof deleteCount === 'number' ? deleteCount : len, len - start),
                0
            );
            // 插入索引值
            delta = insertIndex - deleteCount;
            // 新的长度
            new_len = len + delta;
            // 从开始索引到删除数组项数开始截取
            while (k < deleteCount) {
                element = this[start + k];
                if (element !== undefined) result[k] = element;
                k += 1;
            }
            shift_count = len - start - deleteCount;
            // 如果插入索引值不为0
            if (delta < 0 || delta > 0) {
                k = delta < 0 ? start + insertIndex : 1;
                while (shift_count) {
                    this[delta < 0 ? k : new_len - k] = this[delta < 0 ? k - delta : len - k];
                    k += 1;
                    shift_count -= 1;
                }
                this.length = new_len;
            }
            for (k = 0; k < insertIndex; k++) {
                this[start + k] = arguments[k + 2];
            }
            return result;
        };
        var arr = [1, 2, 3];
        console.log(arr.ArraySplice(0, 1, [1, 2])); //[1]
        console.log(arr) //[[1,2],2,3]
    </script>
</body>

</html>